package cc.orange.mainView;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;

import cc.orange.databinding.ActivitySendDynamicBinding;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.entity.BaseEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ZXToastUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SendDynamicActivity extends BaseActivity {
    private ActivitySendDynamicBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_send_dynamic);
        init();
    }

    private void init() {
        binding.infosetImg3.setOnClickListener(view -> finish());
        binding.tvSubmit.setOnClickListener(view -> {
            report(binding.etContent.getText().toString().trim());
        });
    }

    private void report(String dynamicStr) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), dynamicStr);
        RetrofitFactory.create(ApiService.class)
                .insertDynamic(getTokens(),requestBody )
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(SendDynamicActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if (baseEntity.getCode() == 0) {
//                            EventBus.getDefault().post(new String("pub_refres"));
                            ZXToastUtil.showToast("发布成功，后台正在审核，请耐心等待");
                            finish();
                        } else {
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
//        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }
}
